package com.fduss.artStore.service.impl;

import com.fduss.artStore.dao.ArtworkDao;
import com.fduss.artStore.service.ArtworkService;
import com.fduss.artStore.dao.*;
import com.fduss.artStore.dao.impl.*;
import com.fduss.artStore.entity.*;

import java.util.List;

/**
 * Created by alex on 29/06/2017.
 */
public class ArtworkServiceImpl implements ArtworkService {
    private ArtworkDao artworkDao;
    private ArtworkGenreDao artworkGenreDao;
    private ArtworkSubjectDao artworkSubjectDao;
    private ReviewDao reviewDao;
    private GenreDao genreDao;
    private SubjectDao subjectDao;

    public ArtworkServiceImpl() {
        artworkDao = new ArtworkDaoImpl();
        artworkGenreDao = new ArtworkGenreDaoImpl();
        artworkSubjectDao = new ArtworkSubjectDaoImpl();
        reviewDao =  new ReviewDaoImpl();
        genreDao = new GenreDaoImpl();
        subjectDao = new SubjectDaoImpl();
    }

    @Override
    public Artwork add(Artwork artwork, Genre genre, Subject subject) {
        //add artwork
        artworkDao.add(artwork);

        //add artworkgenre
        ArtworkGenre artworkGenre = new ArtworkGenre();
        artworkGenre.setGenreId(genre.getGenreId());
        artworkGenre.setArtWorkId(artwork.getArtWorkId());
        artworkGenreDao.add(artworkGenre);

        //add artworksubject
        ArtworkSubject artworkSubject = new ArtworkSubject();
        artworkSubject.setArtWorkId(artwork.getArtWorkId());
        artworkSubject.setSubjectId(subject.getSubjectId());
        artworkSubjectDao.add(artworkSubject);
        return artwork;
    }

    @Override
    public Artwork add(Artwork artwork) {
        //todo
        return null;
    }

    @Override
    public void delete(int artworkId) {
        artworkDao.delete(artworkId);
        artworkGenreDao.deleteByArtWork(artworkId);
        artworkSubjectDao.deleteByArtWork(artworkId);
    }

    @Override
    public void update(Artwork artwork) {
        artworkDao.update(artwork);
    }

    @Override
    public int getCount() {
        return artworkDao.getCount();
    }

    @Override
    public Artwork getOne(int artworkId) {
        return artworkDao.getOne(artworkId);
    }

    @Override
    public List<Artwork> search(String key) {
        return artworkDao.getByDecriptionContainingOrTitleContaining(key, key);
    }

    @Override
    public List<Artwork> getSome(int number) {
        int max = artworkDao.getCount();
        int start = (int)(Math.random()*max - number);
        return getOnePage(start/number, number);
    }

    @Override
    public List<Artwork> getOnePage(int pageIndex, int pageSize) {
        return artworkDao.getOnePage(pageIndex, pageSize);
    }

    @Override
    public List<Artwork> getByArtist(int artistId) {
        return artworkDao.getByArtist(artistId);
    }

    @Override
    public List<Artwork> getByGallery(int galleryId) {
        return artworkDao.getByGallery(galleryId);
    }

    @Override
    public List<Review> getReviews(int artworkId) {
        return reviewDao.getByArtWork(artworkId);
    }

    @Override
    public Genre getGenre(int artworkId) {
        return genreDao.getByArtwork(artworkId);
    }

    @Override
    public Subject getSubject(int artworkId) {
        return subjectDao.getByArtwork(artworkId);
    }

    @Override
    public List<Genre> getAllGenres() {
        return genreDao.getAll();
    }

    @Override
    public List<Subject> getAllSubjects() {
        return subjectDao.getAll();
    }
}
